"""
Case Type   : security-EqualityQuery
Case Name   : 加密算法超出枚举范围，创建cmk失败
Create At   : 2024/7/4
Owner       : wan005
Description :
    1.gsql客户端连接数据库加-C参数，创建cmk,加密算法为SM3/sm4
Expect      :
    1.报错：unspported algorithm 'sm3'/'sm4'
History     :
    2024/11/22 modify by @peilinqian: 定位偶现问题单IATFYV，增加调试打印参数
"""
import os
import unittest
from yat.test import Node
from yat.test import macro
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class Auditing(unittest.TestCase):
    def setUp(self):
        self.logger = Logger()
        self.logger.info(f'-----{os.path.basename(__file__)} start-----')
        self.sh_primy = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.userNode = Node('PrimaryDbUser')
        self.cmk = 'cmk_security_qualityquery_0034'
        self.key_path = 'keypath_security_qualityquery_0034'
        self.localkms = os.path.join(macro.DB_INSTANCE_PATH, '..', 'app',
                                     'etc', 'localkms')
    
    def test_security(self):
        text = '----pre1:配置$GAUSSHOME/etc/localkms路径;expect:成功---'
        self.logger.info(text)
        mkdir_cmd = f'if [ ! -d "{self.localkms}" ];' \
            f'then mkdir {self.localkms};fi'
        self.logger.info(mkdir_cmd)
        self.userNode.sh(mkdir_cmd).result()
        text = '---step1:gsql客户端连接数据库加-C参数，创建cmk,加密算法为SM3;' \
               'expect:失败---'
        self.logger.info(text)
        sql_cmd1 = f'drop client master key if exists {self.cmk};' \
            f'create client master key {self.cmk} with ' \
            f'(key_store = localkms, key_path = "{self.key_path}", ' \
            f'algorithm = sm3);' \
            f'create client master key {self.cmk} with ' \
            f'(key_store = localkms, key_path = "{self.key_path}", ' \
            f'algorithm = sm4);'
        msg1 = self.sh_primy.execut_db_sql(sql_cmd1, sql_type='-3 -C')
        self.logger.info(msg1)
        self.assertIn("unspported algorithm 'sm3'", msg1, text + '执行失败')
        self.assertIn("unspported algorithm 'sm4'", msg1, text + '执行失败')

    def tearDown(self):
        text1 = '----删除文件----'
        self.logger.info(text1)
        clean_cmd = f"rm -rf {self.localkms}"
        self.logger.info(clean_cmd)
        clean_msg = self.userNode.sh(clean_cmd).result()
        self.assertEqual('', clean_msg, text1 + '执行失败')
        self.logger.info(f'-----{os.path.basename(__file__)} end-----')
